package com.qingpeng.pay.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.chaychan.library.BottomBarLayout;
import com.qingpeng.pay.R;
import com.qingpeng.pay.adapter.MainFragmentAdapter;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.NoticeBean;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.ToastUtils;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends BaseActivity {

    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.bbl)
    BottomBarLayout bbl;
    private MaterialDialog mDialog;
    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        viewPager.setAdapter(new MainFragmentAdapter(getSupportFragmentManager()));
        bbl.setViewPager(viewPager);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected int getRootViewId() {
        return 0;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    /**
     * 是否已授权
     *
     * @return
     */
    private boolean isNotificationServiceEnable() {
        return NotificationManagerCompat.getEnabledListenerPackages(this).contains(getPackageName());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Call<NoticeBean> getnotice = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).getnotice();
        getnotice.enqueue(new Callback<NoticeBean>() {
            @Override
            public void onResponse(Call<NoticeBean> call, Response<NoticeBean> response) {
                NoticeBean body = response.body();
                if (body.getStatus()!=1){
                    ToastUtils.showToast(MainActivity.this,body.getInfo());
                    return;
                }
                View inflate1 = View.inflate(MainActivity.this, R.layout.dialog_notie, null);
                mDialog = new MaterialDialog.Builder(MainActivity.this).customView(inflate1, false).build();
                mDialog.show();
                TextView textTrue = (TextView) inflate1.findViewById(R.id.text_true);
                TextView textView = (TextView) inflate1.findViewById(R.id.text_message);
                textTrue.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mDialog.dismiss();
                    }
                });
                textView.setText(body.getData().getContent());
            }

            @Override
            public void onFailure(Call<NoticeBean> call, Throwable t) {
                ToastUtils.showToast(MainActivity.this, "网络请求错误,请稍后重试");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean isAuthor = isNotificationServiceEnable();
        if (!isAuthor) {
            //直接跳转通知授权界面
            //android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS是API 22才加入到Settings里，这里直接写死
            startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
        }
    }
}
